package com.helloscala.model

import org.joda.time.DateTime

import net.liftweb.common.Box
import org.bson.types.ObjectId
import org.squeryl.KeyedEntity

import yangbajing.persistence.SquerylEntrypoint._
import yangbajing.util.Imports.Y

object MMicroChat {
  def findById(id: String): Box[MMicroChat] = Y.tryBox {
    transaction(from(Entities.microChats)(mc =>
      where(
        mc.id === id
      ) select (mc)).single)
  }

  def findAll(offset: Int, limit: Int): List[MMicroChat] =
    transaction(from(Entities.microChats)(mc =>
      select(mc) orderBy (mc.created_at.desc)
    ).page(offset, limit).toList)

  def insert(creator: String, content: String): Box[MMicroChat] =
    Y.tryBox {
      val mc = MMicroChat(creator, content)
      transaction(Entities.microChats insert mc)
    }

}

case class MMicroChat(
                       creator: String,
                       content: String,
                       created_at: DateTime = DateTime.now,
                       id: String = ObjectId.get.toString) extends KeyedEntity[String] with MessageTrait
